package com.mindcraft.shop.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.base.utils.CommonConst;
import com.mindcraft.base.utils.JwtHelper;
import com.mindcraft.shop.dto.HotTopicRelativedDto;
import com.mindcraft.shop.dto.MarketDetailSkuComment;
import com.mindcraft.shop.dto.MarketSkuCommentDto;
import com.mindcraft.shop.po.MarketHotTopic;
import com.mindcraft.shop.po.MarketSkuComment;
import com.mindcraft.shop.po.MarketVote;
import com.mindcraft.shop.service.MarketHotTopicService;
import com.mindcraft.shop.service.MarketSkuCommentService;
import com.mindcraft.shop.service.MarketTopicSkuService;
import com.mindcraft.shop.service.MarketVoteService;
import com.mindcraft.shop.utils.LoginCheck;
import com.mindcraft.shop.vo.MarketTopicSkuVo;
import com.mindcraft.shop.vo.MarketVoteVo;
import com.mindcraft.user.utils.MindCraftUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "shop", tags = "海鲜市场接口")
@RestController
@RequestMapping("/shop/market")
public class MarketController {

    @Autowired
    private MarketHotTopicService marketHotTopicService;

    @Autowired
    private MarketTopicSkuService marketTopicSkuService;

    @Autowired
    private MarketVoteService macketVoteService;

    @Autowired
    private MarketSkuCommentService marketSkuCommentService;


    @ApiOperation("查询热榜排行")
    @GetMapping("/listHotTopic")
    public MindCraftResult listHotTopic() {


        //根据人气排序话题.,,如果有label标签，则返回时,label数据随机排序
        LambdaQueryWrapper<MarketHotTopic> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByDesc(MarketHotTopic::getPopularity);
        queryWrapper.last("LIMIT 12");

        List<MarketHotTopic> list = marketHotTopicService.list(queryWrapper);

        // 筛选出有 label 的字段，并将它们存储在新的列表中
        List<MarketHotTopic> topicsWithLabel = list.stream()
                .filter(topic -> topic.getLabel() != null)
                .collect(Collectors.toList());

        // 对带有 label 的字段进行随机排序
        Collections.shuffle(topicsWithLabel);

        // 将随机排序后的数据插入回原始的列表中
        int index = 0;
        for (MarketHotTopic topic : list) {
            if (topic.getLabel() != null) {
                list.set(index, topicsWithLabel.remove(0));
            }
            index++;
        }


        return MindCraftResult.success(list);
    }

    @ApiOperation("热榜点击详情")
    @GetMapping("/getHotRankDetail")
    public MindCraftResult getHotRankDetail() {
        return marketTopicSkuService.getHotRankDetail();
    }


    //查询关联热点
    @ApiOperation("查询关联热点")
    @PostMapping("/getRelativeTopic")
    public MindCraftResult getRelativeTopic(@RequestBody HotTopicRelativedDto hotTopicRelativedDto) {

        List<MarketHotTopic> list = marketHotTopicService.list(
                new LambdaQueryWrapper<MarketHotTopic>()
                        .eq(MarketHotTopic::getRelated, hotTopicRelativedDto.getTopicId())
                        .last("LIMIT " + hotTopicRelativedDto.getCount()));
        if(!CollectionUtils.isEmpty(list)){
            //查询1个或者两个热点
            return MindCraftResult.success(list);
        }else {
            return MindCraftResult.success();
        }

    }

    @ApiOperation("根据topicid分页查询出sku")
    @PostMapping("/listTopicSku")
    public MindCraftResult listTopicSku(@RequestBody MarketTopicSkuVo marketTopicSkuVo) {

        return marketTopicSkuService.getTopicSkuList(marketTopicSkuVo);
    }

    @ApiOperation("根据话题id查询投票")
    @PostMapping("/getVote")
    public MindCraftResult vote(@RequestBody MarketVote marketVote) {
        MarketVote voteData = macketVoteService.getOne(
                new LambdaQueryWrapper<MarketVote>()
                        .eq(MarketVote::getTopicId, marketVote.getTopicId()));
        MarketVoteVo marketVoteVo = new MarketVoteVo();
        if (voteData != null) {
            BeanUtils.copyProperties(voteData, marketVoteVo);

            Integer participateCount = voteData.getParticipateCount();
            if (participateCount != null && participateCount != 0) {
                if (voteData.getCount1() != null) {
                    marketVoteVo.setPercentage1(voteData.getCount1() * 100 / participateCount);
                }
                if (voteData.getCount2() != null) {
                    marketVoteVo.setPercentage2(voteData.getCount2() * 100 / participateCount);
                }
                if (voteData.getCount3() != null) {
                    marketVoteVo.setPercentage3(voteData.getCount3() * 100 / participateCount);
                }
                if (voteData.getCount4() != null) {
                    marketVoteVo.setPercentage4(voteData.getCount4() * 100 / participateCount);
                }
                if (voteData.getCount5() != null) {
                    marketVoteVo.setPercentage5(voteData.getCount5() * 100 / participateCount);
                }
                if (voteData.getCount6() != null) {
                    marketVoteVo.setPercentage6(voteData.getCount6() * 100 / participateCount);
                }
            } else {
                marketVoteVo.setPercentage1(0);
                marketVoteVo.setPercentage2(0);
                marketVoteVo.setPercentage3(0);
                marketVoteVo.setPercentage4(0);
                marketVoteVo.setPercentage5(0);
                marketVoteVo.setPercentage6(0);
            }

            return MindCraftResult.success(marketVoteVo);
        }
        return MindCraftResult.success();
    }

    @ApiOperation("用户点击投票")
    @PostMapping("/voteClick")
    public MindCraftResult voteClick(@RequestBody MarketVote marketVote) {
        // 更新用户选中的投票数量

        MarketVote oldVote = macketVoteService.getOne(new LambdaQueryWrapper<MarketVote>()
                .eq(MarketVote::getTopicId, marketVote.getTopicId()));
        String content = marketVote.getContent();
        if (content.equals(oldVote.getContent1())) {
            oldVote.setCount1(oldVote.getCount1() + 1);
        } else if (content.equals(oldVote.getContent2())) {
            oldVote.setCount2(oldVote.getCount2() + 1);
        } else if (content.equals(oldVote.getContent3())) {
            oldVote.setCount3(oldVote.getCount3() + 1);
        } else if (content.equals(oldVote.getContent4())) {
            oldVote.setCount4(oldVote.getCount4() + 1);
        } else if (content.equals(oldVote.getContent5())) {
            oldVote.setCount5(oldVote.getCount5() + 1);
        } else if (content.equals(oldVote.getContent6())) {
            oldVote.setCount6(oldVote.getCount6() + 1);
        }
        oldVote.setParticipateCount(oldVote.getParticipateCount() + 1);
        macketVoteService.updateById(oldVote);
        return MindCraftResult.success();
    }


    @ApiOperation("海鲜市场详情页根据话题id分页sku列表")
    @PostMapping("/marketDetail/getTopicDetail")
    public MindCraftResult getTopicDetail(@RequestBody MarketTopicSkuVo marketTopicSkuVo) {
        return marketTopicSkuService.getTopicDetail(marketTopicSkuVo);
    }


    @ApiOperation("超话海鲜市场详情页根据innerSort按钮分页sku列表")
    @PostMapping("/marketDetail/getSkuListByInnerSort")
    public MindCraftResult getSortSkuList(@RequestBody MarketTopicSkuVo marketTopicSkuVo) {
        return marketTopicSkuService.getSkuListByInnerSort(marketTopicSkuVo);
    }


    @ApiOperation("话题中分页查询商品评论列表")
    @PostMapping("/marketDetail/getSkuComment")
    public MindCraftResult getSkuCommentSkuList(@RequestBody MarketSkuCommentDto marketSkuCommentDto) {

        return marketSkuCommentService.getSkuCommentSkuList(marketSkuCommentDto);
    }


    @ApiOperation("话题中对商品评论点赞")
    @PostMapping("/marketDetail/likeCommentClick")
    public MindCraftResult likeCommentClick(@RequestBody MarketSkuComment marketSkuComment) {
        LambdaUpdateWrapper<MarketSkuComment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MarketSkuComment::getSkuId, marketSkuComment.getSkuId())
                .eq(MarketSkuComment::getId,marketSkuComment.getId())
                .setSql("like_count = like_count + 1");

        boolean updateResult = marketSkuCommentService.update(updateWrapper);
        if (updateResult) {
            return MindCraftResult.success("点赞成功");
        } else {
            return MindCraftResult.fail("点赞失败");
        }
    }

    @ApiOperation("话题中对商品进行评论")
    @PostMapping("/marketDetail/publishComment")
    @LoginCheck
    public MindCraftResult likeCommentClick(@RequestBody MarketDetailSkuComment marketDetailSkuComment) {


        return marketSkuCommentService.CommentInsertAndgetId(marketDetailSkuComment);

    }

}
